package com.zhaogang.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.zhaogang.dao.SaleAccountMapper;
import com.zhaogang.dto.AdminSaleDto;
import com.zhaogang.entity.Page;
import com.zhaogang.entity.SaleAccount;
import com.zhaogang.service.SaleAccountService;

@Service
public class SaleAccountServiceImpl implements SaleAccountService{
	@Resource
	SaleAccountMapper saleAccountMapper;
	
	@Transactional
	public int registValidate(SaleAccount saleAccount) {
		List<SaleAccount> saleAccountList = saleAccountMapper.selectAllSaleAccountList();
		
		for (SaleAccount saleAccount2 : saleAccountList) {
			if(saleAccount2.getSaleAccount().equals(saleAccount.getSaleAccount())){
				return 0;
			}
		}
		return saleAccountMapper.insertSelective(saleAccount);
	}
	/**
	 * 登录功能
	 */
	public String loginValidate(SaleAccount saleAccount) {
		List<SaleAccount> saleAccountList = saleAccountMapper.selectAllSaleAccountList();
		String msg = "";
		String password = "";
		boolean b = false;
		
		for (SaleAccount saleAccount2 : saleAccountList) {
			if(saleAccount2.getSaleAccount().equals(saleAccount.getSaleAccount())){
				password = saleAccount2.getPassword();
				b = true;
				break;
			}
		}
		
		if(b){
			if(password.equals(saleAccount.getPassword())){
				msg = "success";
			}else{
				msg = "您输入的密码不正确,请重新输入";
			}
		}else{
			msg = "您输入的账号不存在,请重新输入";
		}
		return msg;
		
	}
//	public String loginVAlidatemd5(SaleAccount saleAccount) {
//		// TODO Auto-generated method stub
//		List<SaleAccount> saleAccountList = saleAccountMapper.selectAllSaleAccountList();
//		
//		for (SaleAccount saleAccount2 : saleAccountList) {
//			if(saleAccount2.getSaleAccount().equals(saleAccount.getSaleAccount())){
//				String password_real;
//				password_real=saleAccount2.getPassword();
//				return  password_real;			
//			}
//		}	
//		return null;
//	}
	//根据商户账户名查询返回商户对象
	public SaleAccount loginVAlidatemd5(SaleAccount saleAccount) {
		// TODO Auto-generated method stub
		List<SaleAccount> saleAccountList = saleAccountMapper.selectAllSaleAccountList();
		
		for (SaleAccount saleAccount2 : saleAccountList) {
			if(saleAccount2.getSaleAccount().equals(saleAccount.getSaleAccount())){
		
				return  saleAccount2;			
			}
		}	
		return null;
	}
	
	public List<SaleAccount> selectAllSaleAccountList() {
		List<SaleAccount> list = saleAccountMapper.selectAllSaleAccountList();
		return list;
	}
	public SaleAccount selectByPrimaryKey(Integer id) {
		SaleAccount  saleAccount = saleAccountMapper.selectByPrimaryKey(id);
		return saleAccount;
	}
	
	/**
	 * 查询所有分页
	 */
	public Page findPage(Page page) {
		int totalCount = saleAccountMapper.queryCount();
		Integer pageNow;
		pageNow=page.getPageNow();
		Page p =new Page(totalCount, pageNow);
		p.setTotalCount(totalCount);
		return p;
	}
	
	
	/**
	 * 条件查询分页
	 */
	public Page findPage1(Page page,String saleAccount,String realname) {
		int totalCount = saleAccountMapper.queryCount1(saleAccount,realname);
		Integer pageNow;
		pageNow=page.getPageNow();
		Page p =new Page(totalCount, pageNow);
		p.setTotalCount(totalCount);
		return p;
	}
	
	/**
	 * 查询所有商户
	 */
	public List<AdminSaleDto> adminQuerySale(Page p) {
		return saleAccountMapper.adminQuerySale(p.getStartPos(), p.getPageSize()) ;
	}
	
	/**
	 *  删除商户 
	 */
	public void deleteSale(String saleAccount) {
		saleAccountMapper.deleteSale(saleAccount);
	}
	
	/**
	 *  删除商户 
	 */
	public void resumeSale(String saleAccount) {
		saleAccountMapper.resumeSale(saleAccount);
	}
	
	/**
	 * 条件查询
	 */
	public List<AdminSaleDto> saleQueryByCon(String saleAccount, String realname,Page p ) {
		// TODO Auto-generated method stub
		return saleAccountMapper.saleQueryByCon(saleAccount, realname,p.getStartPos(), p.getPageSize());
	}
	
	
	
}

